package com.atxwq.shop.order.controller;

import com.atxwq.shop.model.dto.h5.OrderInfoDto;
import com.atxwq.shop.model.entity.order.OrderInfo;
import com.atxwq.shop.model.vo.common.Result;
import com.atxwq.shop.model.vo.common.ResultCodeEnum;
import com.atxwq.shop.model.vo.h5.TradeVo;
import com.atxwq.shop.order.mapper.OrderInfoMapper;
import com.atxwq.shop.order.service.OrderInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO
 * @Date 2025/5/23 11:06
 * @Created by 12336
 */

@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 确认下单
     * @return
     */
    @GetMapping("auth/trade")
    public Result<TradeVo> trade(){
        TradeVo tradeVo = orderInfoService.getTrade();
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }


    /**
     * 提交订单
     * @param orderInfoDto
     * @return
     */
    @PostMapping("auth/submitOrder")
    public Result submitOrder(@RequestBody OrderInfoDto orderInfoDto){
        Long orderId = orderInfoService.submitOrder(orderInfoDto);
        return Result.build(orderId, ResultCodeEnum.SUCCESS);

    }

    /**
     * 获取支付页面的订单详细信息
     * @param orderId
     * @return
     */
    @GetMapping("auth/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderId") Long orderId){
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 立即购买
     * @param skuId
     * @return
     */
    @GetMapping("auth/buy/{skuId}")
    public Result<TradeVo> buy(@PathVariable("skuId") Long skuId){
        TradeVo tradeVo = orderInfoService.buy(skuId);
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("auth/{page}/{limit}")
    public Result<PageInfo<OrderInfo>> list(@PathVariable("page") Integer page,
                                            @PathVariable("limit") Integer limit,
                                            @RequestParam(required = false, defaultValue = "") Integer orderStatus){
            PageInfo<OrderInfo> pageInfo = orderInfoService.findOrderByPage(page, limit, orderStatus);
            return  Result.build(pageInfo, ResultCodeEnum.SUCCESS);

    }

    /**
     * 获取订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("auth/getOrderInfoByOrderNo/{orderNo}")
    public Result<OrderInfo> getOrderInfoByOrderNo( @PathVariable("orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getByOrderNo(orderNo) ;
        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

}
